home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / A / AE Sample (TC5) / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-07  |  8.7 KB  |  325 lines  |  [TEXT/KAHL]

  1. /* File: Windows.c */
  2.  
  3. #include    "AESimple.h"
  4.  
  5. void SetScrollMaxima (WindowPtr wp, wiHand    info, Boolean setToZero)
  6. {
  7.     /* Set the maximum values for both the horizontal and vertical scrollbars */
  8.     short    maxVValue = (wp->portRect.bottom - wp->portRect.top) - ((*info)->picBounds.bottom - (*info)->picBounds.top);
  9.     short    maxHValue = (wp->portRect.right - wp->portRect.left) - ((*info)->picBounds.right - (*info)->picBounds.left);
  10.  
  11.     if (maxVValue < 0) maxVValue = 0;
  12.     if (maxHValue < 0) maxHValue = 0;
  13.         
  14.     SetCtlMax((**info).vScrollHand, maxVValue);
  15.     SetCtlMax((**info).hScrollHand, maxHValue);
  16.     
  17.     if (setToZero) {
  18.         SetCtlValue((**info).vScrollHand, 0);
  19.         SetCtlValue((**info).hScrollHand, 0);
  20.     }
  21. } /* SetScrollMaxima */
  22.  
  23.  
  24. void SetControlPositions (WindowPtr wp, wiHand info)
  25. {
  26.     SignedByte     oldState;
  27.     short        portWidth, portHeight;
  28.     Rect        textEditRect, r;
  29.     ControlHandle theControl;
  30.     short        contentIndex;
  31.     short        buttonLeft;
  32.     
  33.     SetPort(wp);
  34.  
  35.     portWidth = wp->portRect.right - wp->portRect.left;
  36.     portHeight = wp->portRect.bottom - wp->portRect.top;
  37.  
  38.     oldState = HGetState((Handle)info);
  39.     MoveHHi((Handle)info);
  40.     HLock((Handle)info);
  41.  
  42.     HideControl((**info).vScrollHand);
  43.     MoveControl((**info).vScrollHand, portWidth - 15, -1);
  44.     SizeControl((**info).vScrollHand, 16, wp->portRect.bottom - 13);
  45.     ShowControl((**info).vScrollHand);
  46.     r = (**(**info).vScrollHand).contrlRect;
  47.     ValidRect(&r);
  48.     
  49.     HideControl((**info).hScrollHand);
  50.     MoveControl((**info).hScrollHand, -1, wp->portRect.bottom - 15);
  51.     SizeControl((**info).hScrollHand, portWidth - 13, 16);
  52.     ShowControl((**info).hScrollHand);
  53.     r = (**(**info).hScrollHand).contrlRect;
  54.     ValidRect(&r);
  55.     
  56.     SetScrollMaxima(wp, info, FALSE);
  57.     HSetState((Handle)info, oldState);
  58. } /* SetControlPositions */
  59.  
  60.  
  61. void NewDisplayWindow ()
  62. {
  63.     WindowPtr    newWindow;
  64.     wiHand        info;
  65.     Rect        controlRect;
  66.     Str255        title;
  67.     Ptr            storage = NewPtrClear(sizeof(WindowRecord));
  68.     Point        winTopLeft = {0, 0};
  69.     
  70.     newWindow = GetNewCWindow(kDisplayWindow, storage, (WindowPtr)-1);
  71.     if (newWindow != NIL) {
  72.         SetPort(newWindow);
  73.         info = (wiHand)NewHandleClear(sizeof(WindowInfo));
  74.         SetWRefCon(newWindow, (long)info);
  75.         HLock((Handle)info);
  76.  
  77.         SetRect(&controlRect, 0, 0, 16, 16);
  78.         (**info).vScrollHand = NewControl(newWindow, &controlRect, NIL, FALSE, 0, 0, 0, scrollBarProc, -2);
  79.         (**info).hScrollHand = NewControl(newWindow, &controlRect, NIL, FALSE, 0, 0, 0, scrollBarProc, -2);
  80.         SetControlPositions(newWindow, info);
  81.         
  82.         PrOpen();
  83.         (**info).printInfo = (THPrint)NewHandleClear(sizeof(TPrint));
  84.         PrintDefault((**info).printInfo);
  85.         PrClose();
  86.  
  87.         HUnlock((Handle)info);
  88.  
  89.         /* Next, we have some tricky code to stagger windows. Basically, if the frontmost */
  90.         /* window is a document window, set the new window to start 10 pixels to the right*/
  91.         /* and 40 pixels below the frontmost window. Otherwise, put the window at (10, 40)*/
  92.         if ((FrontWindow() != 0L) && (((WindowPeek)FrontWindow())->windowKind == userKind))
  93.             winTopLeft = topLeft((**((WindowPeek)FrontWindow())->strucRgn).rgnBBox);
  94.         if (!PtInRect(winTopLeft, &(**GetGrayRgn()).rgnBBox))
  95.             SetPt(&winTopLeft, 0, 0);
  96.          winTopLeft.h += 10;
  97.          winTopLeft.v += 40;
  98.         MoveWindow(newWindow, winTopLeft.h, winTopLeft.v, FALSE);
  99.  
  100.         ShowWindow(newWindow);
  101.     }
  102. } /* NewDisplayWindow */
  103.  
  104.  
  105. void CloseAWindow (WindowPtr whichWindow)
  106. {
  107.     short        wKind;
  108.     wiHand        info;
  109.  
  110.     if (whichWindow) {
  111.         wKind = ((WindowPeek)whichWindow)->windowKind;
  112.         if (wKind < 0)
  113.             CloseDeskAcc(wKind);
  114.         else if (wKind == userKind) {
  115.             info = (wiHand)GetWRefCon(whichWindow);
  116.             DisposeWindow(whichWindow);
  117.             AdjustMenus;
  118.         }
  119.     }
  120. } /* CloseAWindow */
  121.  
  122.  
  123. /*----------------------- Handle window update events ----------------------*/
  124.  
  125.  
  126. void DoUpdate (WindowPtr wp)
  127. {
  128.     Rect         updateRect;
  129.     wiHand        info = (wiHand)GetWRefCon(wp);
  130.     Rect        portRect = wp->portRect;
  131.     Rect        picRect;
  132.     RgnHandle    oldClip = nil;
  133.     Rect        newClip;
  134.     
  135.     SetPort(wp);                         /* make update window active grafPort    */
  136.     BeginUpdate(wp);                    /* visRgn temporarily = updateRgn        */
  137.     EraseRect(&portRect);
  138.     
  139.     oldClip = NewRgn();
  140.     GetClip(oldClip);
  141.     newClip = portRect; newClip.bottom -= 15; newClip.right -= 15;
  142.     ClipRect(&newClip);                    /* restrict drawing to the content area of our window */
  143.     
  144.     if ((*info)->thePicture != NIL) {    /* If we have a picture, draw it */
  145.         picRect = (*info)->picBounds;
  146.         OffsetRect(&picRect, -(**info).currHScrollValue, -(**info).currVScrollValue); /* scroll into place */
  147.         DrawPicture((*info)->thePicture, &picRect);
  148.     } else
  149.         FillRect(&newClip, ltGray);
  150.     SetOrigin(0, 0);
  151.     
  152.     SetClip(oldClip);
  153.     DisposeRgn(oldClip);
  154.     
  155.     DrawControls(wp);
  156.     DrawGrowIcon(wp);
  157.     EndUpdate(wp);                        /* restore normal visRgn of grafport    */
  158. } /* DoUpdate */
  159.  
  160.  
  161. void SetCtlActivate (WindowPtr wp, Boolean activate)
  162. {
  163.     short          hiliteValue;
  164.     ControlHandle currControl;
  165.  
  166.     hiliteValue =  activate ? 0 : 255;
  167.     currControl = ((WindowPeek)wp)->controlList;
  168.     while (currControl) {
  169.         HiliteControl(currControl, hiliteValue);
  170.         currControl = (**currControl).nextControl;
  171.     }
  172. } /* SetCtlActivate */
  173.  
  174.  
  175. void DoActivate (EventRecord *theEvent)
  176. {
  177.     wiHand        info;
  178.     WindowPtr    wp = (WindowPtr)theEvent->message;
  179.  
  180.     AdjustMenus();
  181.     SetPort(wp);
  182.     DrawGrowIcon(wp);
  183.     if (isUserWindow(wp)) {
  184.         info = (wiHand)GetWRefCon(wp);
  185.         SetCtlActivate(wp, theEvent->modifiers & 0x0001);
  186.     }
  187. } /* DoActivate */
  188.  
  189.  
  190.  
  191. void ScrollWindow (WindowPtr wp, wiHand info)
  192. {
  193.     short        vScrollAmount, hScrollAmount;
  194.     short        vScrollValue = GetCtlValue((**info).vScrollHand), /* We use "get" to clip the control value to between 0 and max */
  195.                 hScrollValue = GetCtlValue((**info).hScrollHand);
  196.     Rect        scrollArea      = wp->portRect;
  197.     RgnHandle    scrolledRgn  = NewRgn();
  198.     
  199.     SetPort(wp);
  200.     scrollArea.right -= 15; scrollArea.bottom -= 15;
  201.     
  202.  
  203.     vScrollAmount = (**info).currVScrollValue - vScrollValue;
  204.     hScrollAmount = (**info).currHScrollValue - hScrollValue;
  205.  
  206.     (**info).currVScrollValue = vScrollValue;    /* Make sure our copy of the scrolling info is up to date */
  207.     (**info).currHScrollValue = hScrollValue;
  208.  
  209.     ScrollRect(&scrollArea, hScrollAmount, vScrollAmount, scrolledRgn);
  210.     InvalRgn(scrolledRgn);
  211.     DisposeRgn(scrolledRgn);
  212.     DoUpdate(wp);
  213. } /* ScrollWindow */
  214.  
  215.  
  216.  
  217. pascal void ScrollCallback (ControlHandle whichControl, short partCode)
  218. {
  219.     short        oldValue = GetCtlValue(whichControl);
  220.     short        newValue = 0;
  221.     WindowPtr    wp = (**whichControl).contrlOwner;
  222.     Boolean        vScroll, hScroll;
  223.     Rect        portRect = wp->portRect;
  224.     Point        contentSize;
  225.     wiHand        info;
  226.     
  227.     SetPt(&contentSize,  portRect.bottom - portRect.top - 15,
  228.                          portRect.right - portRect.left - 15);
  229.     info = (wiHand)GetWRefCon(wp);
  230.     oldValue  = GetCtlValue(whichControl);
  231.     vScroll   = (whichControl == (**info).vScrollHand);
  232.     hScroll   = (whichControl == (**info).hScrollHand);
  233.  
  234.     newValue = oldValue;
  235.     switch (partCode) {
  236.         case 0:
  237.         case inThumb:
  238.             /* newValue = oldValue, as above */
  239.             break;
  240.  
  241.         case inUpButton:    /* Arrows scroll 20% */
  242.                 if (vScroll) 
  243.                     newValue -= contentSize.v / 20;
  244.                 else
  245.                     newValue -= contentSize.h / 20;
  246.             break;
  247.  
  248.         case inDownButton: 
  249.                 if (vScroll) 
  250.                     newValue += contentSize.v / 20;
  251.                 else
  252.                     newValue += contentSize.h / 20;
  253.             break;
  254.  
  255.         case inPageUp:         /* Gray areas scroll the window by 80% */
  256.                 if (vScroll) 
  257.                     newValue -= (contentSize.v * 4) / 5;
  258.                 else
  259.                     newValue -= (contentSize.h * 4) / 5;
  260.             break;
  261.  
  262.         case inPageDown: 
  263.                 if (vScroll) 
  264.                     newValue += (contentSize.v * 4) / 5;
  265.                 else
  266.                     newValue += (contentSize.h * 4) / 5;
  267.     }
  268.     
  269.     /* If the control is "pinned", don't let it go any farther */
  270.     if ((oldValue == 0) && (newValue <= 0)) return;
  271.     if ((oldValue == GetCtlMax(whichControl)) && (newValue >= oldValue)) return;
  272.     
  273.     SetCtlValue(whichControl, newValue);
  274.     ScrollWindow(wp, info);
  275. } /* ScrollCallback */
  276.  
  277.  
  278. void DoContentClick (WindowPtr whichWindow, Point globalPt)
  279. {
  280.     Point        localPt;
  281.     ControlHandle whichControl;
  282.     short        part;
  283.     wiHand        info;
  284.  
  285.     if (whichWindow != FrontWindow())
  286.         SelectWindow(whichWindow);
  287.     else {
  288.         SetPort(whichWindow);
  289.         localPt = globalPt;
  290.         GlobalToLocal(&localPt);
  291.         info = (wiHand)GetWRefCon(whichWindow);
  292.         part = FindControl(localPt, whichWindow, &whichControl);
  293.         if (part != 0) {
  294.             if (part == inThumb) {
  295.                 part = TrackControl(whichControl, localPt, NIL);
  296.                 ScrollWindow(whichWindow, info);
  297.             } else
  298.                 part = TrackControl(whichControl, localPt, (ProcPtr)ScrollCallback);
  299.         } else
  300.             SysBeep(5);    /* No clicking on the picture, please */
  301.     }
  302. } /* DoContentClick */
  303.  
  304.  
  305. void DoGrowWindow(WindowPtr whichWindow, EventRecord *theEvent)
  306. {
  307.     Rect        sizeRect;
  308.     long        newSize;
  309.     wiHand        info = (wiHand)GetWRefCon(whichWindow);
  310.     Rect        textEditRect;
  311.     short        textPanelWidth;
  312.  
  313.  
  314.     sizeRect = screenBits.bounds;
  315.     sizeRect.top = 100;
  316.     sizeRect.left = 100;
  317.     newSize = GrowWindow(whichWindow, theEvent->where, &sizeRect);
  318.     if (newSize != 0) {
  319.         SizeWindow(whichWindow, newSize & 0x0000FFFF, newSize >> 16, TRUE);
  320.         SetPort(whichWindow);
  321.         InvalRect(&whichWindow->portRect);
  322.         SetControlPositions(whichWindow, info);
  323.     }
  324. }
  325.